import { computed, inject, reactive, ref } from 'vue'
import Configuration from '@/api/client/configuration.js'
import { SAVED_SUCCESSFULLY } from '@/constants/message.js'

export function useBeginCooperate(id, formRef) {
  const { message } = inject('Feedback')
  const beginCooperateColumns = [
    {
      title: 'Type',
      dataIndex: 'typeName',
      key: 'typeName'
    },
    {
      title: 'New Date',
      dataIndex: 'newDate',
      key: 'newDate',
      customRender: ({ text }) => text || '--'
    },
    {
      title: 'Original Date',
      dataIndex: 'oldDate',
      key: 'oldDate',
      customRender: ({ text }) => text || '--'
    },
    {
      title: 'Reason',
      dataIndex: 'reason',
      key: 'reason'
    },
    {
      title: 'Operate On',
      dataIndex: 'operateOn',
      key: 'operateOn'
    },
    {
      title: 'Operate By',
      dataIndex: 'displayName',
      key: 'displayName'
    }
  ]
  const beginCooperateOperateRecords = ref([])
  const beginCooperateInfo = reactive({
    beginCooperate: null,
    realPaymentDate: null,
    reason: null
  })
  const beginCooperateLoading = ref(true)
  const showBeginCooperateModal = ref(false)
  const beginCooperateRules = computed(() => {
    if (beginCooperateForm.type === 1) {
      return {
        reason: [
          {
            required: true,
            message: 'Please input reason',
            trigger: 'blur'
          }
        ]
      }
    } else {
      return {
        date: [
          {
            required: true,
            message: 'Please select date',
            trigger: 'blur'
          }
        ],
        reason: [
          {
            required: true,
            message: 'Please input reason',
            trigger: 'blur'
          }
        ]
      }
    }
  })
  const beginCooperateForm = reactive({
    type: 1,
    date: undefined,
    reason: undefined
  })
  const loadBeginCooperateInfo = () => {
    Configuration.loadBeginCooperateInfo(id)
      .then(({ data }) => {
        beginCooperateOperateRecords.value = data.log
        beginCooperateInfo.beginCooperate = data.beginCooperate
        beginCooperateInfo.realPaymentDate = data.realPaymentDate
      })
      .finally(() => {
        beginCooperateLoading.value = false
      })
  }
  loadBeginCooperateInfo()
  const handleBeginCooperateRestart = () => {
    beginCooperateForm.type = 1
    beginCooperateForm.date = undefined
    beginCooperateForm.reason = undefined
    showBeginCooperateModal.value = true
  }
  const handleSaveBeginCooperate = () => {
    formRef.value.validate().then(() => {
      beginCooperateLoading.value = true
      Configuration.saveBeginCooperate(id, beginCooperateForm)
        .then(() => {
          loadBeginCooperateInfo()
          message.success(SAVED_SUCCESSFULLY)
        })
        .finally(() => {
          showBeginCooperateModal.value = false
          beginCooperateLoading.value = false
        })
    })
  }
  return {
    beginCooperateInfo,
    beginCooperateLoading,
    showBeginCooperateModal,
    beginCooperateRules,
    beginCooperateForm,
    handleBeginCooperateRestart,
    handleSaveBeginCooperate,
    beginCooperateColumns,
    beginCooperateOperateRecords
  }
}
